var modal_selector = "#modal-table";
var modal_pager = "#modal-pager";
var myEvent;
function addEvent(projectId, event, date, url) {
    jQuery.ajax({
        url: url,
        cache: false,
        type: 'post',
        data: {
            projectId: projectId,
            startDate: date,
            endDate: date,
            title: event.title,
            className: event.className[0],
            businessType: event.type
        },
        success: function (rs) {
            var aj = formatJsonResponse(rs);
            if (aj.code == 500) {
                pushMessage(aj.message, "error", 3);
            }
            $("#calendar").fullCalendar('removeEvents');
            $("#calendar").fullCalendar('refetchEvents');
            $("#calendar").fullCalendar('unselect');
        },
        error: function () {
            pushMessage("系统繁忙，请稍后再试。", "error", 2);
        }
    })
}

function updateEvent(projectId, event, url) {
    jQuery.ajax({
        url: url,
        cache: false,
        type: 'post',
        data: {
            id: event.id,
            startDate: event.start,
            projectId: projectId,
            endDate: event.start,
            businessType: event.type
        },
        success: function (rs) {
            var aj = formatJsonResponse(rs);
            if (aj.code == 500) {
                pushMessage(aj.message, "error", 2);
            }
            $("#calendar").fullCalendar('removeEvents');
            $("#calendar").fullCalendar('refetchEvents');
            $("#calendar").fullCalendar('unselect');
        },
        error: function () {
            pushMessage("系统繁忙，请稍后再试。", "error", 2);
        }
    })
}

function deleteEvent(event, url) {
    jQuery.ajax({
        url: url,
        cache: false,
        type: 'post',
        data: {
            id: event.id
        },
        success: function (rs) {
            var aj = formatJsonResponse(rs);
            if (aj.code == 500) {
                pushMessage(aj.message, "error", 2);
            } else {
                $("#calendar").fullCalendar('removeEvents');
                $("#calendar").fullCalendar('refetchEvents');
                $("#calendar").fullCalendar('unselect');
            }
        },
        error: function () {
            pushMessage("系统繁忙，请稍后再试。", "error", 2);
        }
    })
}

function savePlanDetail(event, url) {
    var _list = {};
    var details = jQuery(modal_selector).jqGrid('getRowData');
    for (var i = 0; i < details.length; i++) {
        if (details[i].status != "") {
            _list["planDetails[" + i + "].id"] = details[i].id;
            _list["planDetails[" + i + "].eventId"] = event.id;
            _list["planDetails[" + i + "].terminalId"] = details[i].terminalId;
            _list["planDetails[" + i + "].cashboxOne"] = details[i].cashboxOne;
            _list["planDetails[" + i + "].cashboxTwo"] = details[i].cashboxTwo;
            _list["planDetails[" + i + "].cashboxThree"] = details[i].cashboxThree;
            _list["planDetails[" + i + "].cashboxFour"] = details[i].cashboxFour;
            _list["planDetails[" + i + "].cashboxFive"] = details[i].cashboxFive;
            _list["planDetails[" + i + "].amount"] = details[i].amount;
            _list["planDetails[" + i + "].businessType"] = event.type;
            _list["planDetails[" + i + "].status"] = details[i].status;
        }
    }
    $("html").showLoading();
    jQuery.ajax({
        url: url,
        cache: false,
        type: 'post',
        dataType: "json",
        data: _list,
        success: function (rs) {
            toastMessage(rs, null);
            $("html").hideLoading();
        },
        error: function () {
            $("html").hideLoading();
            pushMessage("系统繁忙，请稍后再试。", "error", 2);
            return;
        }
    })
}

function copyWeek(projectId, typeList) {
    var year = (new Date()).getFullYear();
    var copyRange = getXDate(year, $("#week_start").spinner("value") + 1);
    var pastRange = getXDate(year, $("#week_end").spinner("value") + 1);
    copyEvents(copyRange, pastRange, projectId, typeList);
}

function copyMonth(projectId, typeList) {
    var year = (new Date()).getFullYear();
    var copyRange = getMonthDateRange(year, $("#month_start").spinner("value"));
    var pastRange = getMonthDateRange(year, $("#month_end").spinner("value"));
    copyEvents(copyRange, pastRange, projectId, typeList);
}

function copyEvents(copyRange, pastRange, projectId, typeList) {
    jQuery.ajax({
        url: root + "/plan/copyEvents",
        cache: false,
        type: 'post',
        dataType: "json",
        data: {
            copyRange: copyRange,
            pastRange: pastRange,
            projectId: projectId
        },
        success: function (rs) {
            toastMessage(rs, root + "/plan/detail.do?id=" + projectId + "&typeList=" + typeList);
        },
        error: function () {
            pushMessage("系统繁忙，请稍后再试。", "error", 2);
            return;
        }
    })
}